﻿using HDSmartCommunity.Models;
using System.Collections.Generic;
using System.Xml;
using System;
using System.Threading.Tasks;
using HDSmartCommunity.BUS;
using HDSmartCommunity.Tool;
using Org.BouncyCastle.Utilities;

namespace HDSmartCommunity.DevicePlatform
{
    public class KoneVoltageSerialData
    {
        public static List<KoneDeviceInfo> kone_list = new List<KoneDeviceInfo>();
        public static List<DeviceStatus> deviceStatuses = new List<DeviceStatus>();
        public static List<CmdInfo> cmdInfos = new List<CmdInfo>();
        public void NetWorkConnect()
        {
            Task t = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    try
                    {
                        if (kone_list.Count > 0)
                        {
                            foreach (var item in kone_list)
                            {
                                if (item.ConnectFlag == false)
                                {
                                    KoneNetDevice koneDevice = new KoneNetDevice();
                                    koneDevice.ConnectServer(item.DeviceIP, item.DevicePort);
                                }
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Log("发送心跳异常" + ex.ToString());
                    }

                    Task.Delay(10000).Wait();
                }
            });
        }

        public void CmdSend()
        {
            Task t = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    try
                    {
                        foreach (var item in kone_list)
                        {
                            if (item.ConnectFlag == true && item.socket != null)
                            {
                                int index = cmdInfos.FindIndex(x => x.DeviceIP == item.DeviceIP && x.DevicePort == item.DevicePort);
                                if (index > -1)
                                {
                                    item.socket.Send(Untils.ToBytes(cmdInfos[index].Cmd));
                                    cmdInfos.RemoveAt(index);
                                    //LogHelper.Log("发送数据" + cmdInfos[index].Cmd);
                                }
                                else
                                {
                                    byte[] bytes = new byte[]
                            {
                                    0x01,0x03,0x00,0x17,0x00,0x06,0x75,0xCC
                            };
                                    item.socket.Send(bytes);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Log("发送命令异常" + ex.ToString());
                    }

                    Task.Delay(200).Wait();
                }
            });
        }

        public void GetAllDevice()
        {
            //创建xml文档
            XmlDocument xmldoc = new XmlDocument();

            //导入指定xml文件
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;//忽略文档里面的注释，注释也是节点，但是无法转换成XmlElement，所以这里不忽略，下面转换就报错
                                           //XmlReader是用liu需要using包起来，读完释放资源，防止占用
            using (XmlReader reader = XmlReader.Create("KoneDeviceInfo.xml", settings))
            {
                xmldoc.Load(reader);
            }

            //获取根节点
            XmlElement areaxml = xmldoc.DocumentElement;

            //遍历根节点的子节点
            foreach (XmlElement area in areaxml.ChildNodes)
            {
                KoneDeviceInfo koneDevice = new KoneDeviceInfo();
                koneDevice.ConnectFlag = false;

                foreach (XmlElement child in area.ChildNodes)
                {
                    if (child.Name == "DeviceName")
                    {
                        koneDevice.DeviceName = child.InnerText;
                    }
                    else if (child.Name == "DeviceIP")
                    {
                        koneDevice.DeviceIP = child.InnerText;
                    }
                    else if (child.Name == "DevicePort")
                    {
                        koneDevice.DevicePort = Convert.ToInt32(child.InnerText);
                    }
                }
                kone_list.Add(koneDevice);
            }
        }

        public void DisChargeSend()
        {
            Task t = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    try
                    {
                        List<DeviceStatus> list = deviceStatuses.FindAll(x => x.BatteryStatus == 2);
                        if (list.Count > 0)
                        {
                            foreach (var item in list)
                            {
                                if (item.BatteryStatus == 2)
                                {
                                    CmdInfo cmdInfo = new CmdInfo();
                                    cmdInfo.Cmd = "01 03 00 0A 00 13 24 05";
                                    cmdInfo.DeviceIP = item.DeviceIP;
                                    cmdInfo.DevicePort = item.DevicePort;
                                    cmdInfos.Add(cmdInfo);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Log("发送命令异常" + ex.ToString());
                    }

                    Task.Delay(3000).Wait();
                }
            });
        }

        public void ChargeSend()
        {
            Task t = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    try
                    {
                        List<DeviceStatus> list = deviceStatuses.FindAll(x => x.BatteryStatus == 1);
                        if (list.Count > 0)
                        {
                            foreach (var item in list)
                            {
                                if (item.BatteryStatus == 1)
                                {
                                    CmdInfo cmdInfo = new CmdInfo();
                                    cmdInfo.Cmd = "01 03 00 0A 00 13 24 05";
                                    cmdInfo.DeviceIP = item.DeviceIP;
                                    cmdInfo.DevicePort = item.DevicePort;
                                    cmdInfos.Add(cmdInfo);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Log("发送命令异常" + ex.ToString());
                    }

                    Task.Delay(60000).Wait();
                }
            });
        }
    }
}
